package com.jhxy.algorithm.minstack;

import java.util.LinkedList;
import java.util.List;

/**
 * leetcode第155题，最小栈_双栈法
 */
public class MinStack {

    List<Integer> stack;
    List<Integer> minStack;

    public MinStack() {
        stack = new LinkedList<>();
        minStack = new LinkedList<>();
    }

    public void push(int val) {
        stack.add(val);
        int minStackValue = Integer.MAX_VALUE;

        if (minStack.size() > 0) {
            minStackValue = minStack.get(minStack.size() - 1);
        }

        minStackValue = Math.min(minStackValue, val);

        minStack.add(minStackValue);
    }

    public void pop() {
        stack.remove(stack.size() - 1);
        minStack.remove(minStack.size() - 1);
    }

    public int top() {
        return stack.get(stack.size() - 1);
    }

    public int getMin() {
        return minStack.get(minStack.size() - 1);
    }

}
